const {
  updataUserInfo
} = require("../../utils/http/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: "",
    nickName: "",
    intro: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this
    async function show() {
      await _this.getimutUser()
    }
    show()
  },
  // 获取用户基本信息
  getimutUser() {
    let user = wx.getStorageSync('userInfo')
    this.setData({
      id:  user.id,
      avatarUrl: user.avatarImage,
      nickName: user.nickname,
      intro: user.signature,
      phoneNumber: user.phoneNumber,
      email:user.email
    })
  },
  // 图片预览
  /**
* 图片预览
* @param {*} e 
*/
  previewImage: function (e) {
    var current = e.target.dataset.src;
    var imgList = [];
    imgList.push(this.data.avatarUrl);
    imgList.push(this.data.cover)
    wx.previewImage({
      current: current,//当前点击的图片链接
      urls: imgList//图片数组
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    let _this = this
    // console.log("压缩前",avatarUrl)
    // 压缩图片
    wx.compressImage({
      src: avatarUrl, // 图片路径
      quality: 50, // 压缩质量
      success: (res) => {
        // console.log("压缩后",res.tempFilePath)
        _this.setData({
          avatarUrl: res.tempFilePath,
        })
        wx.uploadFile({
          url: uploadUrl, //仅为示例，非真实的接口地址
          filePath: res.tempFilePath,
          name: 'file',
          header: {
            token: wx.getStorageSync('userInfo').token
          },
          success(res) {
            console.log("文件上传结果", JSON.parse(res.data))
            try {
              let user = wx.getStorageSync('userInfo')
              user.info.avatar = JSON.parse(res.data).data
              wx.setStorageSync('userInfo', user)
            } catch (error) {

            }
          }
        })
      }
    })
    // 上传图片


  },
  onChooseCover() {
    wx.chooseImage({
      count: 1,
      success: (res) => {
        console.log(res.tempFilePaths[0])
        let _this = this
        // console.log("压缩前",avatarUrl)
        // 压缩图片
        wx.compressImage({
          src: res.tempFilePaths[0], // 图片路径
          quality: 50, // 压缩质量
          success: (res) => {
            // console.log("压缩后",res.tempFilePath)
            _this.setData({
              cover: res.tempFilePath,
            })
            wx.uploadFile({
              url: uploadUrl, //仅为示例，非真实的接口地址
              filePath: res.tempFilePath,
              name: 'file',
              header: {
                token: wx.getStorageSync('userInfo').token
              },
              success(res) {
                console.log("文件上传结果", JSON.parse(res.data))
                try {
                  let user = wx.getStorageSync('userInfo')
                  user.info.cover = JSON.parse(res.data).data
                  wx.setStorageSync('userInfo', user)
                } catch (error) {

                }
              }
            })
          }
        })
      }
    })
  },
  // 昵称修改
  nickNamechange(e) {
    // console.log(e.detail.value)
    this.setData({
      nickName: e.detail.value
    })
  },
  // 个人签名修改
  introchange(e) {
    // console.log(e.detail.value)
    this.setData({
      intro: e.detail.value
    })
  },
  // 个人签名修改
  inPhoneNumber(e) {
    // console.log(e.detail.value)
    this.setData({
     phoneNumber: e.detail.value
    })
  },
  // 用户信息判断
  judgeMessage() {
    let _this = this
    let username = this.data.nickName
    let intro = this.data.intro
    if (username.length > 10) {
      wx.showToast({
        title: '超出十个字符了哦~',
        icon: "none"
      })
    } else if (intro.length > 18) {
      wx.showToast({
        title: '超出18个字符了哦~',
        icon: "none"
      })
    } 
     _this.postUser()
  },
  // 用户信息上传
  postUser() {
    let data = {
      id: this.data.id,
      avatarImage: this.data.avatarUrl,
      nickname: this.data.nickName,
      signature: this.data.intro,
      phoneNumber: this.data.phoneNumber,
      email: this.data.email
    }
    
    console.log("data",data)
    updataUserInfo(data).then((res) => {
      if (res.code == 200) {
        wx.showToast({
          title: '修改成功',
          icon: "none"
        })
      } else {
        wx.showToast({
          title: '稍后再试',
          icon: "error"
        })
      }
    })
    wx.switchTab({
      url: '/pages/myself/myself',
      success: function () {
        // 触发页面重新加载
        let page = getCurrentPages().pop();
        if (page == undefined || page == null) return;
        page.onShow();
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})